package com.zztl.infiniteBand.ui.view.dialog


import android.app.Dialog
import android.content.Context
import android.support.annotation.StyleRes
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.adapter.DeviceAdapter
import com.zztl.infiniteBand.model.BluetoothDeviceBean
import com.zztl.infiniteBand.ui.view.recycleview.SimpleItemDecoration
import java.util.*


/**
 * @desc
 * @auth ${user}
 * @time 2018/1/9  15:12
 */

class DeviceDialog : Dialog {

    class Builder(private val context: Context) {
        private var message: String? = null
        private var isShowMessage = true
        private var isCancelable = false
        private var isCancelOutside = false
        private var cancelListener: ClickListener? = null
        private var deviceList: List<BluetoothDeviceBean>? = null
        private var mItemClickListener: ItemClickListener? = null

        /**
         * 设置提示信息
         *
         * @param message
         * @return
         */

        fun setMessage(message: String): Builder {
            this.message = message
            return this
        }

        /**
         * 设置是否显示提示信息
         *
         * @param isShowMessage
         * @return
         */
        fun setShowMessage(isShowMessage: Boolean): Builder {
            this.isShowMessage = isShowMessage
            return this
        }

        /**
         * 设置是否可以按返回键取消
         *
         * @param isCancelable
         * @return
         */

        fun setCancelable(isCancelable: Boolean): Builder {
            this.isCancelable = isCancelable
            return this
        }

        /**
         * 设置是否可以取消
         *
         * @param isCancelOutside
         * @return
         */
        fun setCancelOutside(isCancelOutside: Boolean): Builder {
            this.isCancelOutside = isCancelOutside
            return this
        }

        fun setClickListener(listener: ClickListener): Builder {
            this.cancelListener = listener
            return this
        }

        fun setListData(deviceList: ArrayList<BluetoothDeviceBean>): Builder {
            this.deviceList = deviceList
            return this
        }

        fun setItemClickListener(itemClickListener: ItemClickListener): Builder {
            this.mItemClickListener = itemClickListener
            return this
        }


        fun create(): DeviceDialog {

            val inflater = LayoutInflater.from(context)
            val view = inflater.inflate(R.layout.layout_dialog_device, null)
            val dialog = DeviceDialog(context, R.style.CommomDialog)

            view.findViewById<TextView>(R.id.title)?.apply {
                if (isShowMessage) {
                    this.text = message
                } else {
                    this.visibility = View.GONE
                }
            }

            view.findViewById<TextView>(R.id.cancel).apply {
                this.setOnClickListener {
                    dialog.dismiss()
                    cancelListener?.cancel()
                }
            }


            view.findViewById<RecyclerView>(R.id.rv)?.apply {
                layoutManager = LinearLayoutManager(context)
                addItemDecoration(SimpleItemDecoration(context))
                deviceAdapter = DeviceAdapter(R.layout.item_device, deviceList)
                adapter = deviceAdapter
            }
            deviceAdapter?.let { deviceAdapter ->
                deviceAdapter.setOnItemClickListener { _, _, position ->
                    if (deviceAdapter.getItem(position)!!.isConnecting) {
                        return@setOnItemClickListener
                    }
                    mItemClickListener?.let {
                        deviceAdapter.getItem(position)?.let { bean ->
                            bean.isConnecting = true
                            deviceAdapter.setData(position, bean)
                        }
                        it.itemClick(position)
                    }
                }
            }
            dialog.setContentView(view)
            dialog.setCancelable(isCancelable)
            dialog.setCanceledOnTouchOutside(isCancelOutside)
            return dialog

        }


        interface ClickListener {
            fun cancel()

            fun comfirm()
        }


        interface ItemClickListener {
            fun itemClick(position: Int)
        }

    }


    fun resetUi() {
        val data = deviceAdapter?.data
        for (i in data!!.indices) {
            data[i].isConnected = false
            data[i].isConnecting = false
        }
        deviceAdapter!!.notifyDataSetChanged()
    }

    fun setConnectedUi(position: Int) {
        deviceAdapter?.let {
            it.getItem(position)?.isConnected = true
            it.getItem(position)?.isConnecting = false
//            it.setData(position, it.getItem(position)!!)
            it.notifyItemChanged(position)
        }

        dismiss()
    }


    fun showDialog() {
        val window = this.window
        val lp = window!!.attributes
        /*
         * 设置宽高，不设置默认根据内容自适应
         */
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window.attributes = lp
        show()
    }

    constructor(context: Context) : super(context)


    constructor(context: Context, @StyleRes themeResId: Int) : super(context, themeResId)


    companion object {

        private var deviceAdapter: DeviceAdapter? = null
    }

}
